package com.qf.android.huihui.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.qf.android.huihui.R;
import com.qf.android.huihui.adapter.shopping.ShoppingAdapter;
import com.qf.android.huihui.bean.show.shoppingbottom.ShoppingBase;
import com.qf.android.huihui.common.IClickListern;
import com.qf.android.huihui.common.InfoRecyclerView;
import com.qf.android.huihui.presenter.ShoppingPresenter;
import com.qf.android.huihui.view.activity.WebviewActivity;
import com.qf.android.huihui.view.fragment.impl.BaseFragment;
import com.qf.android.huihui.view.fragment.impl.IShowView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShoppingFragment extends BaseFragment implements IShowView ,PullToRefreshBase.OnRefreshListener2<RecyclerView>{
    private InfoRecyclerView recyclerView;
    private ShoppingPresenter presenter;
    private ShoppingAdapter adapter;
    private List<ShoppingBase> list=new ArrayList<>();
    public ShoppingFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_shopping, container, false);
        recyclerView= (InfoRecyclerView) view.findViewById(R.id.fragmenr_shop_recyclerview);
        recyclerView.setMode(PullToRefreshBase.Mode.BOTH);
        LinearLayoutManager manager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.getRefreshableView().setLayoutManager(manager);
        recyclerView.setOnRefreshListener(this);
        presenter=new ShoppingPresenter(this);
        list.clear();
        presenter.getData(null,null,false,false,null,null);
        return view;
    }

    @Override
    public void setData(Object data) {
        List<ShoppingBase> lists=(List<ShoppingBase>)data;
        lists.subList(0,lists.size()/2).clear();
        if(list.size()==0){
            list.addAll(lists);
        }else {
            recyclerView.getRefreshableView().scrollToPosition(list.size()-1);
            list.addAll(lists);
        }

        adapter=new ShoppingAdapter(list,getContext());
        adapter.setMyClickListener(new IClickListern() {
            @Override
            public void OnItemClick(View view, String url) {
                Intent intent = new Intent(getContext(), WebviewActivity.class);
                intent.putExtra("url", url);
                getContext().startActivity(intent);
            }
        });
        recyclerView.getRefreshableView().setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
        list.clear();
        presenter.getData(null,null,false,true,adapter,recyclerView);
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<RecyclerView> refreshView) {
       presenter.getData(adapter,recyclerView);
    }
}
